/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.synjones.pay.bank.api;

import com.synjones.core.util.MapUtil;
import com.synjones.pay.model.common.ResponseBean;
import com.synjones.pay.model.context.IPayContext;
import com.synjones.pay.model.context.StatusInfo;

import java.util.Map;

/**
 *
 * @author zhzy
 */
public abstract class AbstractBankJumpImpl implements IBankJump {

    private static final String JUMP_URL = "jumpUrl";

    @Override
    public abstract Map<String, String> buildSendData(IPayContext ctx);

    @Override
    public abstract String getJumpUrl();

    @Override
    public Map<String, String> doPay(IPayContext ctx) {
        Map<String, String> reqMap = MapUtil.newInstance();
        reqMap.putAll(this.buildSendData(ctx));
        reqMap.put(JUMP_URL, this.getJumpUrl());
        return reqMap;
    }

    @Override
    public ResponseBean buildRespBean(Map<String, String> resMap) {
        if (this.verifyRespSign(resMap)) {
            return this.resolveRespData(resMap);
        } else {
            return genSignFaildBean();
        }
        //throw new RuntimeException("签名错误");
    }

    /**
     * 签名失败返回的对象
     *
     * @return
     */
    private ResponseBean genSignFaildBean() {
        ResponseBean.Builder builder = new ResponseBean.Builder();
        builder.statusCode(StatusInfo._4001.code());
        ResponseBean responseBean = builder.builder();
        return responseBean;
    }

}
